// pages/login/welcome.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    code: '',
    id: '',
    sid: '',
    sss: 0 //分享码
  },
  login() {
    wx.navigateTo({
      url: '/pages/login/login'
    })
  },


  // 回到首页
  returnInto: function() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // let getid = decodeURIComponent(options.scene)
    let getid = options.scene
    console.log('options', options)
    console.log('scene', options.scene)
    console.log('解析', decodeURIComponent(options))
    console.log('sid', decodeURIComponent(options.scene))
    if (options.sss != undefined) {
      this.setData({
        sss: options.sss
      })
    }
    if (getid != undefined) {
      this.setData({
        sid: getid
      })
    } else {
      this.setData({
        sid: 0
      })
    }
    wx.getUserInfo({
      success: res => {
        console.log(res)
        app.globalData.userInfo = res.userInfo
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true,
        })
      }
    })

  },
  getUserInfo: function(e) {
    let that = this
    console.log('getuser', e, e.detail.rawData)
    // 传code

    let nickname = e.detail.userInfo.nickName
    let avatarurl = e.detail.userInfo.avatarUrl
    that.setData({

      nickname: nickname,
      avatarurl: avatarurl,

    })
    console.log(that.data.nickname, that.data.avatarurl)
    // phone: that.data.mobile,
    //   nickname: that.data.name,
    //     openid: that.data.id,
    //       avatarurl: that.data.photo,
    //         nickname: that.data.name,
    //           sex: that.data.sex,
    if (e.detail.userInfo) {
      wx.login({
        success: function(res) {
          console.log(res)
          wx.request({
            url: app.globalData.api + "authlogin",
            data: {
              code: res.code,
              sid: that.data.sid,
              nickname: that.data.nickname,
              avatarurl: that.data.avatarurl
            },
            success: function(res) {
              console.log('登录', res)
              if (that.data.sss == 0) {
                if (res.data.status == 0) {
                  wx.navigateTo({
                    url: '/pages/login/phonelogin/phonelogin?nickname=' + e.detail.userInfo.nickName + '&avatarurl=' + e.detail.userInfo.avatarUrl + '&gender=' + e.detail.userInfo.gender + '&id=' + res.data.openid + '&sid=' + that.data.sid
                  })
                } else {

                  wx.setStorage({
                    key: 'userinfo',
                    data: res.data.list,
                  })
                  wx.switchTab({
                    url: '/pages/index/index',
                  })
                }
              } else {
                wx.setStorageSync('userinfo', res.data.list)
                wx.navigateBack({
                  delta: 1
                })
              }

            },
          })
        }
      })
      app.globalData.userInfo = e.detail.userInfo
      that.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })
      console.log(that.data.userInfo)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})